import { TransactionService } from '../transaction.service';

export function Transactional() {
  return function (
    _target: any,
    _propertyKey: string,
    descriptor: PropertyDescriptor,
  ) {
    const originalMethod = descriptor.value;
    descriptor.value = async function (...args: any[]) {
      const transactionService: TransactionService = this['transactionService'];
      if (!(transactionService instanceof TransactionService)) {
        throw new Error(
          'TransactionService not found. Make sure it is properly injected.',
        );
      }
      return transactionService.runInTransaction((queryRunner) =>
        originalMethod.apply(this, [...args, queryRunner]),
      );
    };
    return descriptor;
  };
}
